home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / Login2.aspx.cs401 < prev    next >
Text File  |  2007-12-08  |  15KB  |  366 lines

  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Xml;
  8. using GBPVR.Public;
  9.  
  10. namespace gbweb
  11. {
  12.     /// <summary>
  13.     /// Summary description for Login.
  14.     /// </summary>
  15.     public partial class Login2 : Page
  16.     {
  17.         protected string salt;
  18.         private string requiredGuestPasswordHash;
  19.         private String requiredUsername;
  20.         private String requiredPasswordHash;
  21.         bool use_new_themes = false;
  22.         private string hostAddress = string.Empty;
  23.         protected Settings guideParams;
  24.     
  25.         protected void Page_Load(object sender, EventArgs e)
  26.         {
  27.             getTheme();
  28.             Session["GuideStartTime"] = null;
  29.             Session["NotGuestUser"] = "true";
  30.  
  31.             Logger.Info("Loading Config...");
  32.             XmlDocument configDoc = Global.Config;
  33.  
  34.             // extract username and password
  35.             requiredUsername = "admin";
  36.             requiredPasswordHash = string.Empty;
  37.             XmlNode node = configDoc.SelectSingleNode("/settings/WebUsername");
  38.             if (node != null)
  39.             {
  40.                 requiredUsername = node.InnerText;
  41.             }
  42.             node = configDoc.SelectSingleNode("/settings/WebPassword");
  43.             if (node != null)
  44.             {
  45.                 requiredPasswordHash = node.InnerText;
  46.             }
  47.             guideParams = Global.Settings;
  48.             requiredGuestPasswordHash = guideParams.GuestPassword;
  49.             Logger.Info(" ");
  50.             Logger.Info("Pulled Credential from Config...");
  51.             Logger.Info("User= " + requiredUsername);
  52.             Logger.Info("Password = " + requiredPasswordHash);
  53.             Logger.Info(" ");
  54.             if (!IsPostBack)
  55.             {
  56.                 Logger.Info("Postback not found....now pulling User Host Info...");
  57.                 if (Request.UserHostAddress.Substring(0, 1) == ":")
  58.                 {
  59.                     Logger.Info("IP V6 Detected on Server ");
  60.                     if (Global.Settings.autologinNets.CheckNumber(GetIP4Address()))
  61.                     {
  62.                         hostAddress = GetIP4Address();
  63.                         Logger.Info("Using IP4 Address: " + hostAddress);
  64.                         Logger.Info(" ");
  65.                         CompleteLogin();
  66.                     }
  67.                 }
  68.                 else
  69.                 {
  70.                     Logger.Info("IP V4 Detected on Server");
  71.                     if (Global.Settings.autologinNets.CheckNumber(Request.UserHostAddress))
  72.                     {
  73.                         hostAddress = Request.UserHostAddress;
  74.                         Logger.Info("Using IP4 Address: " + hostAddress);
  75.                         Logger.Info(" ");
  76.                         CompleteLogin();
  77.                     }
  78.                 }
  79.             }
  80.             Logger.Info(" ");
  81.             Logger.Info("Setting logo attribute to version info...");
  82.             //logo.Attributes.Add("title", Global.Settings.GetVersionToolTip());
  83.             Logger.Info("Establishing login salt");
  84.             // allocate salt if we havn't already
  85.             if (Session["LoginSalt"] == null)
  86.             {
  87.                 Session["LoginSalt"] = Guid.NewGuid().ToString();
  88.             }
  89.             salt = (string)Session["LoginSalt"];
  90.             Logger.Info("Login salt = " + salt);
  91.             Logger.Info("Registering hidden field...LoginButton.UniqueID = " + LoginButton.UniqueID);
  92.             Logger.Info(" ");
  93.             Page.RegisterHiddenField("__EVENTTARGET", LoginButton.UniqueID);
  94.  
  95.         }
  96.  
  97.         #region Web Form Designer generated code
  98.         override protected void OnInit(EventArgs e)
  99.         {
  100.             //
  101.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  102.             //
  103.             InitializeComponent();
  104.             base.OnInit(e);
  105.         }
  106.         
  107.         /// <summary>
  108.         /// Required method for Designer support - do not modify
  109.         /// the contents of this method with the code editor.
  110.         /// </summary>
  111.         private void InitializeComponent()
  112.         {    
  113.             this.ID = "LoginButton";
  114.  
  115.         }
  116.         #endregion
  117.  
  118.         protected void LoginButton_Click(object sender, EventArgs e)
  119.         {
  120.             Logger.Info("Loginbutton click detected...");
  121.             // check credentials
  122.  
  123.  
  124.             // generate the expected hash
  125.             string expectedHash = FormsAuthentication.HashPasswordForStoringInConfigFile(requiredPasswordHash.ToLower() + salt, "MD5");
  126.             string GuestexpectedHash = FormsAuthentication.HashPasswordForStoringInConfigFile(requiredGuestPasswordHash.ToLower() + salt, "MD5");
  127.  
  128.  
  129.             // direct user to "manage listings" if credentials are correct
  130.             invalidCredentialsLabel.Visible = true;                
  131.             Logger.Info("Now pulling User Host Info...");
  132.             if (Request.UserHostAddress.Substring(0, 1) == ":")
  133.             {
  134.                 Logger.Info("IP V6 Detected");
  135.                 hostAddress = GetIP4Address();
  136.                 Logger.Info("IP6 Address: " + Request.UserHostAddress);
  137.                 Logger.Info("IP4 Address: " + hostAddress);
  138.             }
  139.             else
  140.             {
  141.                 Logger.Info("IP V4 Detected");
  142.                 hostAddress = Request.UserHostAddress;
  143.                 Logger.Info("IP4 Address: " + hostAddress);
  144.             }
  145.  
  146.             Logger.Info(" ");
  147.             Logger.Info("Loading Global Settings.... ");
  148.             Logger.Info(" ");
  149.             guideParams = Global.Settings;
  150.             Logger.Info(" ");
  151.             Session["NotGuestUser"] = "true";
  152.             Logger.Info("Checking to see if the username and password that were entered match what was pulled from config....");
  153.             if ((Username.Text == requiredUsername) &&
  154.                 (PasswordHash.Value == expectedHash.ToLower()))
  155.             {
  156.                 Logger.Info("User name and password matched for Administrative access...");
  157.                 Logger.Info(" ");
  158.                 CompleteLogin();
  159.             }
  160.             else
  161.             {
  162.                 // Create a StringComparer an comare the hashes.
  163.                 StringComparer comparer = StringComparer.OrdinalIgnoreCase;
  164.  
  165.                 if ((0 == comparer.Compare(PasswordHash.Value, GuestexpectedHash)) &&
  166.                     Username.Text == guideParams.GuestUser)
  167.                 {
  168.                     Session["NotGuestUser"] = "false";
  169.                     Logger.Info("User name and password matched for Guest access...");
  170.                     Logger.Info(" ");
  171.                     CompleteLogin();
  172.                 }
  173.                 else
  174.                 {
  175.                     Logger.Warning("Failed login for " + Username.Text + " from " + hostAddress);
  176.                     Logger.Info("Pasword hash value = " + PasswordHash.Value);
  177.                     Logger.Info("Expected Admin hash value = " + expectedHash.ToLower());
  178.                     Logger.Info("Expected Guest hash value = " + guideParams.GuestPassword);
  179.                 }
  180.             }
  181.         }
  182.  
  183.         private void CompleteLogin()
  184.         {
  185.             Logger.Info("You have made it to the complete login method....this is a good thing.... ");
  186.  
  187.             string userName = Username.Text;
  188.             bool createPersistentCookie = false;
  189.             string cookiePath = FormsAuthentication.FormsCookiePath;
  190.             string redirectUrl = FormsAuthentication.GetRedirectUrl(userName, createPersistentCookie);
  191.             Uri redirectUri = new Uri(Request.Url, redirectUrl);
  192.  
  193.             Logger.Info("Checking if new theme flag is set.... ");
  194.             if (!use_new_themes)
  195.             {
  196.                 Logger.Info("New theme flag is not set... ");
  197.                 Logger.Info("Checking username not blank, redirect uri = request path... ");
  198.                 if ((Username.Text.Length == 0) && (
  199.                          (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Admin2.aspx", true) == 0)))
  200.                 {
  201.                     Logger.Info("User name was blank and the redirect uri and request path were set to admin2.aspx... ");
  202.                     Logger.Info(" ");
  203.                     return;
  204.                 }
  205.                 Logger.Info("Checking to see if the redirec uri is equal to default.aspx or logout.aspx ");
  206.                 if (
  207.                     (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Default.aspx", true) == 0) ||
  208.                     (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Logout2.aspx", true) == 0))
  209.                 {
  210.                     Logger.Info("Redirect uri was equal to default.aspx or logout.aspx....seting the redirect to guide.aspx...... ");
  211.                     Logger.Info(" ");
  212.                     redirectUrl = Request.ApplicationPath + "/Guide2.aspx";
  213.                 }   
  214.                 else
  215.                 {
  216.                     Logger.Info("Redirect uri was not equal to default.aspx or logout.aspx.... ");
  217.                     Logger.Info(" ");
  218.                 }
  219.             }
  220.             else
  221.             {
  222.                 Logger.Info("New theme flag is set...");
  223.                 Logger.Info("Checking username not blank, redirect uri = request path... ");
  224.                 if ((Username.Text.Length == 0) && (
  225.                         (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Admin2.aspx", true) == 0)))
  226.                 {
  227.                     Logger.Info("User name was blank and the redirect uri and request path were set to admin2.aspx... ");
  228.                     Logger.Info(" ");
  229.                     return;
  230.                 }
  231.                 Logger.Info("Checking to see if the redirec uri is equal to default2.aspx or logout2.aspx ");
  232.                 if (
  233.                     (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Default.aspx", true) == 0) ||
  234.                     (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Logout2.aspx", true) == 0))
  235.                 {
  236.                     Logger.Info("Redirect uri was equal to default2.aspx or logout2.aspx....seting the redirect to guide2.aspx...... ");
  237.                     Logger.Info(" ");
  238.                     redirectUrl = Request.ApplicationPath + "/Guide2.aspx";
  239.                 }  
  240.                 else
  241.                 {
  242.                     Logger.Info("Redirect uri was not equal to default2.aspx or logout2.aspx.... ");
  243.                     Logger.Info(" ");
  244.             }
  245.             }
  246.             Logger.Info(" ");
  247.             if (Username.Text.Length == 0)
  248.             {
  249.                 Logger.Info("Automatic login from " + hostAddress);
  250.                 Session["NotGuestUser"] = "true";
  251.                 Logger.Info(" ");
  252.  
  253.  
  254.             }
  255.             else
  256.             {
  257.                 Logger.Info("Successful login for " + Username.Text + " from " + hostAddress);
  258.                 Logger.Info(" ");
  259.             }
  260.  
  261.             Logger.Info("Initializing Forms Authentication.... ");
  262.             Logger.Info(" ");
  263.             FormsAuthentication.Initialize();
  264.             Logger.Info("Setting Forms Authentication Cookie.... ");
  265.             Logger.Info("Username = " + Username.Text);
  266.             Logger.Info("Cookie Path = " + cookiePath);
  267.             Logger.Info(" ");
  268.             FormsAuthentication.SetAuthCookie(Username.Text, createPersistentCookie, cookiePath);
  269.  
  270.             Logger.Info("Checking for auto search execution.... ");
  271.             if ((guideParams.autoShowSearch || guideParams.autoShowRecord) && (String.Compare(guideParams.lastAutoSearchDate, DateTime.Now.ToShortDateString()) != 0))
  272.             {
  273.                 Logger.Info("Auto Search found and has not yet been executed today.... ");
  274.                 XmlNode EPGHour = Global.Config.SelectSingleNode("/settings/UpdateEPGHour");
  275.                 Logger.Info("Checking if it is at least one hour past the EPG update for today.... ");
  276.                 if (DateTime.Now.Hour + 1 > Convert.ToInt32(EPGHour.InnerText))
  277.                 {
  278.                     Logger.Info("It is greater than one hour since todays EPG update.... ");
  279.                     Logger.Info("Executing auto search.... ");
  280.                     SavedSearchUtils searchUtil = new SavedSearchUtils();
  281.                     searchUtil.autoSearch(searchUtil.loadAutoSearches());
  282.                     Logger.Info("Setting last auto search date to today so this routine does not run again until tommorow.... ");
  283.                     guideParams.lastAutoSearchDate = DateTime.Now.ToShortDateString();
  284.                     guideParams.Save();
  285.                 }
  286.                 else
  287.                 {
  288.                     Logger.Info("Auto search can not execute now since it is not 1 hour greater than todays EPG update.... ");
  289.                 }
  290.             }
  291.             else
  292.             {
  293.                 Logger.Info("Auto Search is not set to run or it has aleady run today.... ");
  294.             }
  295.             Logger.Info(" ");
  296.             Logger.Info("Redirecting to the target page.... ");
  297.             Logger.Info("Redirect URL = " + redirectUrl);
  298.             Response.Redirect(redirectUrl, false);
  299.             Logger.Info(" ");
  300.         }
  301.  
  302.         public string GetIP4Address()
  303.         {
  304.             string strIP4Address = String.Empty;
  305.  
  306.             foreach (IPAddress objIP in Dns.GetHostAddresses(Dns.GetHostName()))
  307.             {
  308.                 if (objIP.AddressFamily.ToString() == "InterNetwork")
  309.                 {
  310.                     strIP4Address = objIP.ToString();
  311.                     break;
  312.                 }
  313.             }
  314.             return strIP4Address;
  315.         }
  316.  
  317.         private void getTheme()
  318.         {
  319.             use_new_themes = false;
  320.             string themeDir = Server.MapPath(Request.ApplicationPath);
  321.             if (File.Exists(Path.Combine(themeDir, "select_theme.xml")))
  322.             {
  323.                 XmlDocument configThemes = new XmlDocument();
  324.                 configThemes.Load(Path.Combine(themeDir, "select_theme.xml"));
  325.                 XmlNodeList themeConfig = configThemes.SelectNodes("theme_config");
  326.                 foreach (XmlNode themeOpt in themeConfig)
  327.                 {
  328.                     use_new_themes = Convert.ToBoolean(themeOpt.SelectSingleNode("//use_new_themes").InnerText);
  329.                 }
  330.             }
  331.  
  332.             string theme = Convert.ToString(Session["theme"]);
  333.  
  334.             HttpCookie cookie = new HttpCookie("UseNewThemes", use_new_themes.ToString());
  335.             cookie.Expires = DateTime.Now.AddYears(1);
  336.             Response.Cookies.Add(cookie);
  337.  
  338.             if (theme != null && theme != "" && !use_new_themes)
  339.             {
  340.                 return;
  341.             }
  342.             else
  343.             {
  344.                 cookie = Request.Cookies["theme"];
  345.                 if (cookie != null && cookie.Value.Length > 0)
  346.                 {
  347.                     theme = cookie.Value;
  348.                 }
  349.                 else
  350.                 {
  351.                     theme = "Default";
  352.                 }
  353.                 if (use_new_themes)
  354.                 {
  355.                     Session["theme"] = "themes2/" + theme;
  356.                 }
  357.                 else
  358.                 {
  359.                     Session["theme"] = "themes2/" + theme;
  360.                 }
  361.                 return;
  362.             }
  363.         }
  364.     }
  365. }
  366.